/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.agenda.model.persistence;

import com.agenda.model.entity.AmTbConvMedi;
import com.agenda.model.repository.ConvenioMedicoRepository;
import java.util.List;
import javax.persistence.Query;

/**
 *
 * @author Ronildo
 */
public class ConvenioMedicoDAO extends GenericDAO<AmTbConvMedi, ConvenioMedicoRepository> implements ConvenioMedicoRepository {
    
    public AmTbConvMedi findById(Long id){
        return (AmTbConvMedi)em.createNamedQuery("AmTbConvMedi.findByCdConvMedi").
                setParameter("cdConvMedi", id).getSingleResult();
    }

    public List<AmTbConvMedi> list(AmTbConvMedi convenioMedico){
        try{
            String hql = "select convenioMedico from AmTbConvMedi as convenioMedico ";
            hql+=" where convenioMedico.cdConvMedi = convenioMedico.cdConvMedi";
            
            if(convenioMedico.getNmConvMedi() != null && !convenioMedico.getNmConvMedi().equals(""))
                    hql+=" and convenioMedico.nmConvMedi = :nmConvMedi";
            hql+=" order by convenioMedico.cdConvMedi";

            Query query = em.createQuery(hql);
            if(convenioMedico.getNmConvMedi() != null && !convenioMedico.getNmConvMedi().equals(""))
                    query.setParameter("nmConvMedi", convenioMedico.getNmConvMedi());
            
            return query.getResultList();
        }catch(RuntimeException e){
            throw new DAOException(e);
        }
    }

    public List<AmTbConvMedi> listAll(){
        try{
            return em.createNamedQuery("AmTbConvMedi.findAll").getResultList();
        }catch(IllegalStateException e){
            throw new DAOException(e);
        }
    }
    
}
